<?php
class MY_Pagination extends CI_Pagination{

	var $js_function = '';
	var $next_link = 'Next';
	var $prev_link = 'Prev';
	var $first_link = '«First';
	var $last_link = 'Last»';
	var $show_total = 'no';
	var $show_text_total = 'of';
	var $show_num = 'yes';
	var $show_button = 'yes';
	var $show_first_last = 'yes';
	var $first_class	=	'prevnext';
	var $last_class		=	'prevnext';
	var $next_class 	= 	'next';
	var $prev_class 	=	'prev';
	var $num_class 		= 	'number';
	var $cur_class 		= 	'currentpage';
	var $title_link 	=	'Page';
	var $total_tag_open = '';
	var $total_tag_close = '';
	var $page_string = 'p';
	var $num_space		=	'&nbsp;|';
	
	function __construct($params = array())
	{
		parent::__construct($params);
	}
	
	function display_ajax(){
		$pagination = '';
		$total = ciel($this->total_rows / $this->per_page);
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $total) 
				{
					$end = $total;
					$first = $total - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $total) {$end = $total;}
			}
			
			$pagination.= $this->full_tag_open;
				if($this->show_button == 'yes'){
					if($this->cur_page>1)
					{
						if($this->show_first_last == 'yes'){
							$pagination .=	$this->first_tag_open;
							$pagination .= "<a href='javascript:void(0)' onclick='".$this->js_function."(1".")'>".$this->first_link."</a>";
							$pagination .=	$this->first_tag_close;
						}
						
						$pagination .=	$this->prev_tag_open;
						$pagination .= "<a href='javascript:void(0)' class='".$this->prev_class."' onclick='".$this->js_function."(".($this->cur_page-1).")'>".$this->prev_link."</a>";
						$pagination .=	$this->prev_tag_close;
					}
				}
				//else
				//{
					//$pagination.=	$this->first_tag_open;
					//$pagination.=	'<span>'.$this->first_link.'</span>';
					//$pagination.=	$this->first_tag_close;
					
					//$pagination.=	$this->prev_tag_open;
					//$pagination.=	'<span>'.$this->prev_link.'</span>';
					//$pagination.=	$this->prev_tag_close;
					
				//}
				
				if($this->show_num == 'yes'){
					for($i = $first; $i <= $end; $i++)
					{
						if($i==$this->cur_page)
						{
							$pagination .= $this->num_tag_open;
							$pagination .= '<b>'.$i.'</b>';
							$pagination .= $this->num_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							$pagination .= "<a href='javascript:void(0)' class='number' onclick='".$this->js_function."(".$i.")'>".$i."</a>";
							$pagination .= $this->num_tag_close;
						}
					}
				}
				
				if($this->show_total == 'yes') $pagination .= '<span> '.$this->show_text_total.'&nbsp;'.$total.'</span>';
				
				if($this->show_button == 'yes'){
					if($this->cur_page<$total)
					{
						
							$pagination.= $this->next_tag_open;
							$pagination.=	"<a href='javascript:void(0)' class='".$this->next_class."' onclick='".$this->js_function."(".($this->cur_page+1).")'>".$this->next_link."</a>";
							$pagination.= $this->next_tag_close;
						
						if($this->show_first_last == 'yes'){
							$pagination.=	$this->last_tag_open;
							$pagination.= "<a href='javascript:void(0)' onclick='".$this->js_function."(".$total.")'>".$this->last_link."</a>";
							$pagination.=	$this->last_tag_close;
						}
					}
				}
				//else
				//{
					//$pagination .= $this->next_tag_open;
					//$pagination .= '<span>'.$this->next_link.'</span>';
					//$pagination .= $this->next_tag_close;
					
					//$pagination .=	$this->last_tag_open;
					//$pagination .=	'<span>'.$this->last_link.'</span>';
					//$pagination .=	$this->last_tag_close;
				//}
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function ajax_url(){
		$pagination = '';
		$total = ceil($this->total_rows / $this->per_page);
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $total) 
				{
					$end = $total;
					$first = $total - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $total) {$end = $total;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					$pagination .=	$this->first_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."')>".$this->first_link."</a>";
					$pagination .=	$this->first_tag_close;
					
					$pagination .=	$this->prev_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page-1)."')>".$this->prev_link."</a>";
					$pagination .=	$this->prev_tag_close;
				}
				
				for($i = $first; $i <= $end; $i++)
				{
					
					if($i==$this->cur_page)
					{
						$pagination .= $this->num_tag_open;
						$pagination .= '<a class="currentpage">'.$i.'</a>';
						$pagination .= $this->num_tag_close;
					}
					else
					{
						$pagination .= $this->num_tag_open;
						$pagination .= "<a href='javascript:void(0)' class='number' onclick=".$this->js_function."('".$this->base_url."/".$i."')>".$i."</a>";
						$pagination .= $this->num_tag_close;
					}
					
				}
				
				if($this->show_total == 'yes') $pagination .= '<span> '.$this->show_text_total.'&nbsp;'.$total.'</span>';
				
				if($this->cur_page<$total)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page+1)."')>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					$pagination.=	$this->last_tag_open;
					$pagination.= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".$total."')>".$this->last_link."</a>";
					$pagination.=	$this->last_tag_close;
				}
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function ajax_query_string(){
		$this->base_url = preg_replace("/(&|\?)".$this->page_string."=[0-9]?/","",$this->base_url);

		$pagination = '';
		$total = ceil($this->total_rows / $this->per_page);
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $total) 
				{
					$end = $total;
					$first = $total - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $total) {$end = $total;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					$pagination .=	$this->first_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."')>".$this->first_link."</a>";
					$pagination .=	$this->first_tag_close;
					
					$pagination .=	$this->prev_tag_open;
					$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".((strpos($this->base_url,"?")==false) ? $this->base_url."?".$this->page_string."=".($this->cur_page-1) : $this->base_url."&".$this->page_string."=".($this->cur_page-1))."')>".$this->prev_link."</a>";
					$pagination .=	$this->prev_tag_close;
				}
				
				for($i = $first; $i <= $end; $i++)
				{
					
					if($i==$this->cur_page)
					{
						$pagination .= $this->num_tag_open;
						$pagination .= '<a class="currentpage">'.$i.'</a>';
						$pagination .= $this->num_tag_close;
					}
					else
					{
						$pagination .= $this->num_tag_open;
						$pagination .= "<a href='javascript:void(0)' class='number' onclick=".$this->js_function."('".((strpos($this->base_url,"?")==false) ? $this->base_url."?".$this->page_string."=".$i : $this->base_url."&".$this->page_string."=".$i)."')>".$i."</a>";
						$pagination .= $this->num_tag_close;
					}
					
				}
				
				if($this->show_total == 'yes') $pagination .= '<span> '.$this->show_text_total.'&nbsp;'.$total.'</span>';
				
				if($this->cur_page<$total)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a href='javascript:void(0)' onclick=".$this->js_function."('".((strpos($this->base_url,"?")==false) ? $this->base_url."?".$this->page_string."=".($this->cur_page+1) : $this->base_url."&".$this->page_string."=".($this->cur_page+1))."')>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					$pagination.=	$this->last_tag_open;
					$pagination.= "<a href='javascript:void(0)' onclick=".$this->js_function."('".((strpos($this->base_url,"?")==false) ? $this->base_url."?".$this->page_string."=".$total : $this->base_url."&".$this->page_string."=".$total)."')>".$this->last_link."</a>";
					$pagination.=	$this->last_tag_close;
				}
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function display_ajax2(){
		$first = 0;
		$end = 0;
		if ($this->cur_page != 1) 
		{
			$first = $this->cur_page - 3;
			if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
			$end = $end + $this->cur_page + 3;
			if ($end > $total) 
			{
				$end = $total;
				$first = $total - 6;
				if ($first <= 0) {$first = 1;}
			}
		}
		else {
			$first = 1;$end = 7;
			if ($end > $total) {$end = $total;}
		}
		
		$pagination = '';
		
		$pagination.= $this->full_tag_open;
			
			if($this->cur_page>1)
			{
				$pagination .=	$this->first_tag_open;
				$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/1'".")>".$this->first_link."</a>";
				$pagination .=	$this->first_tag_close;
				
				$pagination .=	$this->prev_tag_open;
				$pagination .= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page-1)."')>".$this->prev_link."</a>";
				$pagination .=	$this->prev_tag_close;
			}
			else
			{
				$pagination.=	$this->first_tag_open;
				$pagination.=	'<span>'.$this->first_link.'</span>';
				$pagination.=	$this->first_tag_close;
				
				$pagination.=	$this->prev_tag_open;
				$pagination.=	'<span>'.$this->prev_link.'</span>';
				$pagination.=	$this->prev_tag_close;
				
			}
			
			for($i = $first; $i <= $end; $i++)
			{
				
				if($i==$this->cur_page)
				{
					$pagination .= $this->num_tag_open;
					$pagination .= '<a id="current" class="number">'.$i.'</a>';
					$pagination .= $this->num_tag_close;
				}
				else
				{
					$pagination .= $this->num_tag_open;
					$pagination .= "<a href='javascript:void(0)' class='number' onclick=".$this->js_function."('".$this->base_url."/".$i."')>".$i."</a>";
					$pagination .= $this->num_tag_close;
				}
				
			}
			if($this->cur_page<$total)
			{
				$pagination.= $this->next_tag_open;
				$pagination.=	"<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".($this->cur_page+1)."')>".$this->next_link."</a>";
				$pagination.= $this->next_tag_close;
				
				$pagination.=	$this->last_tag_open;
				$pagination.= "<a href='javascript:void(0)' onclick=".$this->js_function."('".$this->base_url."/".$total."')>".$this->last_link."</a>";
				$pagination.=	$this->last_tag_close;
			}
			else
			{
				$pagination .= $this->next_tag_open;
				$pagination .= '<span>'.$this->next_link.'</span>';
				$pagination .= $this->next_tag_close;
				
				$pagination .=	$this->last_tag_open;
				$pagination .=	'<span>'.$this->last_link.'</span>';
				$pagination .=	$this->last_tag_close;
			}
			
		$pagination.= $this->full_tag_close;	
		return $pagination;
	}
	
	function display(){
		$pagination = '';
		$total = ceil($this->total_rows/$this->per_page);
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $total) 
				{
					$end = $total;
					$first = $total - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $total) {$end = $total;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					if($this->show_first_last == 'yes'){
						$pagination .=	$this->first_tag_open;
						$pagination .= "<a href='".$this->base_url."/"."'>".$this->first_link."</a>";
						$pagination .=	$this->first_tag_close;
					}
					
					$pagination .=	$this->prev_tag_open;
					if(($this->cur_page-1)==1)
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url."/"."'>".$this->prev_link."</a>".$this->num_space;
					else
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url."/". ($this->cur_page-1) ."/'>".$this->prev_link."</a>".$this->num_space;
					$pagination .=	$this->prev_tag_close;
				}
				
				if($this->show_num == 'yes'){
					$j=0;
					for($i = $first; $i <= $end; $i++)
					{
						$j++;
						if($i==$this->cur_page)
						{
							$pagination .= $this->cur_tag_open;
							$pagination .= '<a class="'.$this->cur_class.'">'.$i.'</a>'.$this->num_space;
							$pagination .= $this->cur_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							if($i==1)
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url."/"."'>".$i."</a>".$this->num_space;
							else
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url."/".$i."/'>".$i."</a>".$this->num_space;
							$pagination .= $this->num_tag_close;
						}
						
					}
				}else{
					if($this->cur_page > 1 && $this->cur_page < $total)
						$pagination .= $this->num_space;
				}
				
				if($this->show_total == 'yes') $pagination .= $this->total_tag_open.'of '.$total.$this->total_tag_close;
				
				if($this->cur_page<$total)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a class='".$this->next_class."' href='".$this->base_url."/".($this->cur_page+1)."/'>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					if($this->show_first_last == 'yes'){
						$pagination.=	$this->last_tag_open;
						$pagination.= "<a href='".$this->base_url."/".$total."/'>".$this->last_link."</a>";
						$pagination.=	$this->last_tag_close;
					}
				}
				
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
	
	function display_query_string(){
		$this->base_url = preg_replace('@'.$this->page_string.'=[0-9]+@is', '', $this->base_url);
		$this->base_url = str_replace(array('&&','?&'), array('&','?'), $this->base_url);
		$this->base_url = preg_replace('@(&|\?)$@is', '', $this->base_url);
		
		$pagination = '';
		$total = ceil($this->total_rows / $this->per_page);
		if($total > 1){
			$first = 0;
			$end = 0;
			if ($this->cur_page != 1) 
			{
				$first = $this->cur_page - 3;
				if ($first<=0) {$first = 1;$end = 4 -  $this->cur_page;}
				$end = $end + $this->cur_page + 3;
				if ($end > $total) 
				{
					$end = $total;
					$first = $total - 6;
					if ($first <= 0) {$first = 1;}
				}
			}
			else {
				$first = 1;$end = 7;
				if ($end > $total) {$end = $total;}
			}
			
			$pagination.= $this->full_tag_open;
				
				if($this->cur_page>1)
				{
					if($this->show_first_last == 'yes'){
						$pagination .=	$this->first_tag_open;
						$pagination .= "<a class='".$this->first_class."' href='".$this->base_url.""."'>".$this->first_link."</a>";
						$pagination .=	$this->first_tag_close;
					}
					
					$pagination .=	$this->prev_tag_open;
					if(($this->cur_page-1)==1)
						$pagination .= "<a class='".$this->prev_class."' href='".$this->base_url.""."'>".$this->prev_link."</a>".$this->num_space;
					else
						$pagination .= "<a class='".$this->prev_class."' href='".((strpos($this->base_url,'?')==false) ? $this->base_url."?".$this->page_string."=". ($this->cur_page-1) : $this->base_url."&".$this->page_string."=". ($this->cur_page-1)) ."'>".$this->prev_link."</a>".$this->num_space;
					$pagination .=	$this->prev_tag_close;
				}
				
				if($this->show_num == 'yes'){
					$j=0;
					for($i = $first; $i <= $end; $i++)
					{
						$j++;
						if($i==$this->cur_page)
						{
							$pagination .= $this->cur_tag_open;
							$pagination .= '<a class="'.$this->cur_class.'">'.$i.'</a>'.$this->num_space;
							$pagination .= $this->cur_tag_close;
						}
						else
						{
							$pagination .= $this->num_tag_open;
							if($i==1)
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".$this->base_url.""."'>".$i."</a>".$this->num_space;
							else
								$pagination .= "<a title='".$this->title_link.' '.$i."' class='".$this->num_class.(($j==7) ? ' end' : '')."' href='".((strpos($this->base_url,'?')==false) ? $this->base_url."?".$this->page_string."=".$i : $this->base_url."&".$this->page_string."=".$i) ."'>".$i."</a>".$this->num_space;
							$pagination .= $this->num_tag_close;
						}
						
					}
				}else{
					if($this->cur_page > 1 && $this->cur_page < $total)
						$pagination .= '|';
				}
				
				if($this->show_total == 'yes') $pagination .= $this->total_tag_open.'of '.$total.$this->total_tag_close;
				
				if($this->cur_page < $total)
				{
					$pagination.= $this->next_tag_open;
					$pagination.=	"<a class='".$this->next_class."' href='".((strpos($this->base_url,'?')==false) ? $this->base_url."?".$this->page_string."=".($this->cur_page+1) : $this->base_url."&".$this->page_string."=".($this->cur_page+1)) ."'>".$this->next_link."</a>";
					$pagination.= $this->next_tag_close;
					
					if($this->show_first_last == 'yes')
					{
						$pagination.=	$this->last_tag_open;
						$pagination.= "<a class='".$this->last_class."' href='".((strpos($this->base_url,"?")==false) ? $this->base_url."?".$this->page_string."=".$total : $this->base_url."&".$this->page_string."=".$total)."'>".$this->last_link."</a>";
						$pagination.=	$this->last_tag_close;
					}
				}
				else
				{
					$pagination .= $this->next_tag_open;
					$pagination .= '<span>'.$this->next_link.'</span>';
					$pagination .= $this->next_tag_close;
					
					if($this->show_first_last == 'yes'){
						$pagination .=	$this->last_tag_open;
						$pagination .=	'<span>'.$this->last_link.'</span>';
						$pagination .=	$this->last_tag_close;
					}
				}
				
				
			$pagination.= $this->full_tag_close;
		}	
		return $pagination;
	}
}